package com.bw.auth.consumer;

import com.bw.auth.domain.SysUser;
import com.bw.auth.service.SysUserService;
import com.bw.feign.CropFeignService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@RocketMQMessageListener(topic = "demo6",consumerGroup = "group1")
public class RocketMqConsumer implements RocketMQListener {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CropFeignService cropFeignService;
    @Autowired
    private HttpServletResponse response;
    @Override
    public void onMessage(Object o) {
        System.err.println(o);
        //查询管理员
       List<SysUser> sysUsers = sysUserService.getUserRole();
       cropFeignService.pdf();
        System.out.println("nvvnfnvfvffasong");
       List<String> list = new ArrayList<>();
       for (SysUser sysUser : sysUsers) {
           if (!list.contains(sysUser.getEmail())){
               MimeMessage mimeMessage = javaMailSender.createMimeMessage();
               try {
                   MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
                   helper.setFrom("19825701655@163.com");
                   helper.setTo(sysUser.getEmail());
                   helper.setSubject("仓储盘点通知");
                   helper.setText("管理员"+sysUser.getUname()+"您好，本周仓储盘点最新数据已发送");

                   File file = new File("D:\\pdf\\sign\\3.pdf");
                   helper.addAttachment("3.pdf",file);
                   javaMailSender.send(mimeMessage);
                   System.out.println("nvvnfnvfvf");
                   list.add(sysUser.getEmail());

               } catch (MessagingException e) {
                   throw new RuntimeException(e);
               }
           }
       }

    }
}
